package com.controller;

import com.bo.Conversation;
import com.request.GptSendRequest;
import com.response.ConversationDTO;
import com.response.ConversationRes;
import com.service.GptService;
import com.utils.BaseResponse;
import com.utils.R;
import com.utils.TripleDTO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/gpt")
public class GptController {

    @Resource
    GptService gptService;


    @PostMapping("/send")
    @ApiOperation("查询文心一言")
    public BaseResponse<ConversationRes> query(@RequestBody GptSendRequest req, HttpServletRequest request) throws IOException {
        ConversationRes res = gptService.queryWxyy(req, (Long) request.getSession().getAttribute("userId"));
        return BaseResponse.ok(res);
    }


    @GetMapping("/all_history")
    @ApiOperation("获取用户对话历史")
    public BaseResponse<List<TripleDTO<String, String, List<Conversation>>>> history(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("userId");
        List<TripleDTO<String, String, List<Conversation>>> userQueryHistory = gptService.getUserQueryHistory(userId);
        return BaseResponse.ok(userQueryHistory);
    }

    @GetMapping("/history")
    @ApiOperation("获取用户对话历史")
    public BaseResponse<List<ConversationDTO>> history(@Valid @NotNull Long contextId) {
        List<ConversationDTO> userQueryHistory = gptService.queryHistoryDTO(contextId);
        return BaseResponse.ok(userQueryHistory);
    }

    @GetMapping("/new_conversation")
    @ApiOperation("开启新对话")
    public BaseResponse<Long> newConversation(HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute("userId");
        Long contextId = gptService.getUserNewestContextId(userId);
        return BaseResponse.ok(contextId);
    }
}
